package live.zs;

import io.netty.util.internal.StringUtil;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import live.zs.config.Conversions;
import live.zs.config.Converters;
import live.zs.config.Optionals;
import live.zs.config.Server;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.util.Optional;

@Path("/hello")
public class ExampleResource {

    @ConfigProperty(name = "greeting.message")
    String message;

    @ConfigProperty(name = "greeting.suffix", defaultValue = "!")
    String suffix;

    @ConfigProperty(name = "greeting.name")
    Optional<String> name;

    @Inject
    Server server;
    @Inject
    Conversions conversions;
    @Inject
    Optionals optionals;
    @Inject
    Converters converters;


    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello RESTEasy " + message + " " + name.orElse("world") + suffix;
    }

    Logger logger = LoggerFactory.getLogger(ExampleResource.class);

    /**
     * 编程方式读取配置
     *
     * @return
     */
    @GET
    @Path("hi")
    @Produces(MediaType.TEXT_PLAIN)
    public String hi() {
        String databaseName = ConfigProvider.getConfig().getValue("database.name", String.class);
        Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("database.name", String.class);

        // map to object
//        SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
//        Server server = config.getConfigMapping(Server.class);
        logger.info(server.host());
        logger.info(server.hostAndPort().host());
        logger.info(server.log().suffix());
        logger.info(server.thePort());
        logger.info(conversions.booleanWrapper());
        logger.info(conversions.longPrimitive());
        logger.info(optionals.optional().get());
        logger.info(optionals.server().get().port());
        logger.info(converters.foo());

        return StringUtil.isNullOrEmpty(databaseName) ? "" : databaseName + " " + maybeDatabaseName.orElse("no config database");
    }
}
